﻿using Data0426.Base.Common;
using Data0426.Base.Dommain;
using Data0426.Base.Infrastructure;
using Data0426.Base.Read.API.Applaction.Command;
using MD5Hash;
using MediatR;

namespace Data0426.Base.Read.API.Applaction.Hander
{
    public class LoginHander : IRequestHandler<LoginCommand, ApiResult<Accounts>>
    {
        private readonly IBaseRequest<Accounts> rAccountsequest;

        public LoginHander(IBaseRequest<Accounts> rAccountsequest)
        {
            this.rAccountsequest = rAccountsequest;
        }

        public Task<ApiResult<Accounts>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<Accounts>();
            var info = rAccountsequest.GetAll().Where(x => x.AccountName == request.AccountName).FirstOrDefault();
            if(info==null)
            {
                result.code = ApiEnumcode.Faile;
                result.meg = "用户名不存在";
            }
            else
            {
                if (info.AccountPassword != request.AccountPassword.GetMD5())
                {
                    result.data = info;
                    result.code = ApiEnumcode.Faile;
                    result.meg = "密码不对";
                }
                else
                {
                    result.data = info;
                    result.code = ApiEnumcode.OK;
                    result.meg = "登录成功";
                }
            }
            return Task.FromResult(result);
        }
    }
}
